/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package a1A;

/**
 *
 * @author nisarg
 */
public class water_shield {
    int[][] input = {{1,2,3},{4,5,6},{7,8,9}};
    int[][] input1 = {{0,0,0},{0,0,0},{0,0,0}};
    int[][] input2 = {{0,0,0},{0,0,0},{0,0,0}};

    int val1 = 1;
    //water_shield obj = new water_shield();
    public static void main(String args[]) {
        int t = 10;
        water_shield a = new water_shield();
        int i1=0,j1=0,min=a.input[0][0];
        for(t=0;t<9;t++) {
            min = a.input[0][0];
            for(int i=0;i<3;i++) {
                for(int j=0;j<3;j++) {
                    if(min < a.input[i][j] && a.input2[i][j] == 0) {
                        i1 = i;
                        j1 = j;
                        min = a.input[i][j];
                    }
                    /*else if(a.input2[i][j]!=0) {
                        min = a.input[0][0];
                    }*/
                }
            }
            a.input2[i1][j1] = 1;
            a.min1(i1,j1,min);
        }
        for(int i=0;i<3;i++) {
            for(int j=0;j<3;j++)
                System.out.print(a.input1[i][j]);
            System.out.println();
        }
        System.out.println();
        System.out.println();
        System.out.println();
        for(int i=0;i<3;i++) {
            for(int j=0;j<3;j++)
                System.out.print(a.input2[i][j]);
            System.out.println();
        }
    }
    void min1(int i,int j,int val) {
        int min = val;
        int index;
        int mina=-1,minb=-1;
        //input[i][j] = val1++;
        if( (j-1)>=0 && min > input[i][j-1] ) {
            min = input[i][j-1];
            //index = i*10+(j+1);
            mina = i;
            minb = j-1;
        }
        if((i-1) >= 0 && min>input[i-1][j]) {
            min = input[i-1][j];
            //index = i*10+(j+1);
            mina = i-1;
            minb = j;
        }
        if((i+1) < 3   && min>input[i+1][j]) {
            min = input[i+1][j];
            //index = i*10+(j+1);
            mina = i+1;
            minb = j;
        }
        if((j+1) < 3  && min>input[i][j+1]) {
            min = input[i][j+1];
            //index = i*10+(j+1);
            mina = i;
            minb = j+1;
        }
        /*if(mina!=-1 && minb!=-1) {*/
        //System.out.println("mina" + mina + "minb = " + minb);
            if(mina!=-1 && minb != -1 && mina!=i && minb != j && input1[mina][minb] == 0 && input1[mina][minb] == 0) {
                System.out.println("first val =" + val);
                input1[i][j] = val1;
                input1[mina][minb] = val1++;
            }   
            else if(mina!=-1 && minb != -1) {
                replace(input[i][j],input[mina][minb]);
            }
        }
    void replace(int a,int b) { //a is original value, to be replaced with b.
        for(int i=0;i<3;i++) {
            for(int j=0;j<3;j++) {
                if(input1[i][j]==a) {
                    input1[i][j] = b;
                }
            }
        }
    }
}